package com.wsq.annotationprocesslib;

import com.google.auto.service.AutoService;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import com.wsq.annotationlib.javapoet.HelloWorld;


import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

@AutoService(Processor.class)
public class HelloWorldProcess extends AbstractProcessor {
    private Filer filer;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        System.out.println("HelloWorldProcess init");
        filer = processingEnv.getFiler();
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "hello, apt");
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(HelloWorld.class);
        for (Element element : elements) {

            Name simpleName = element.getSimpleName();
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,simpleName);
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,"chiji");
        }


        for(TypeElement element:annotations){
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "print element name:" + element.getQualifiedName());
            System.out.println("print element name:" + element.getSimpleName());
            if (element.getQualifiedName().toString().equals(HelloWorld.class.getCanonicalName())){

                Thread thread;

                MethodSpec main = MethodSpec.methodBuilder("main")
                        .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                        .returns(void.class)
                        .addParameter(String[].class, "args")
                        .addStatement("$T.out.println($S)", System.class, "Hello, World!")
                        .build();

                MethodSpec test = MethodSpec.methodBuilder("test")
                        .addModifiers(Modifier.PUBLIC)
                        .returns(void.class)
                        .addParameter(String[].class, "args")
                        .addStatement("$T.out.println($S)", System.class, "Hello, World!")
                        .build();

                TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld11")
                        .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                        .addMethod(main)
                        .addMethod(test)
                        .build();

                JavaFile javaFile = JavaFile.builder("com.wushengqi", helloWorld)
                        .addFileComment("test")
                        .build();
                try {
                    javaFile.writeTo(filer);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return false;
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "getSupportedAnnotationTypes");
        Set<String> annotations = new LinkedHashSet<>();
        annotations.add(HelloWorld.class.getCanonicalName());
        return annotations;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "getSupportedSourceVersion");
        return SourceVersion.latestSupported();
    }
}
